package 算法.NiuKe.JZ;

import 算法.NiuKe.ListNode;

import java.util.ArrayList;

/**
 * @author: 谢君臣
 * @create: 2021-01-20 12:52
 * @founction:
 * 输入一个链表，按链表从尾到头的顺序返回一个ArrayList。
 * 输入
 * {67,0,24,58}
 * 返回值
 * [58,24,0,67]
 */
public class JZ3 {
    public static void main(String[] args) {
        ListNode head = new ListNode(0);
        for (int i = 0; i < 5; i++) {
            ListNode p = head;
            while (p.next!=null)p = p.next;
            ListNode node = new ListNode((int) (Math.random()*100));
            p.next = node;
        }
        printListFromTailToHead(head);
    }
    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        if (listNode==null)
            return list;
        ListNode newNode = new ListNode(listNode.val);
        while (listNode.next!=null){
            listNode = listNode.next;
            ListNode head = new ListNode(listNode.val);
            head.next = newNode;
            newNode = head;
        }
        while (newNode!=null){
            list.add(newNode.val);
            newNode = newNode.next;
        }
        return list;
    }
}
